package com.huiminpay.merchant.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BizException;
import com.huiminpay.merchant.convert.MerchantConvert;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.entity.Staff;
import com.huiminpay.merchant.entity.Store;
import com.huiminpay.merchant.entity.StoreStaff;
import com.huiminpay.merchant.mapper.MerchantMapper;
import com.huiminpay.merchant.mapper.StaffMapper;
import com.huiminpay.merchant.mapper.StoreMapper;
import com.huiminpay.merchant.mapper.StoreStaffMapper;
import com.huiminpay.merchant.service.IMerchantService;
import com.huiminpay.user.api.TenantService;
import com.huiminpay.user.api.dto.tenant.CreateTenantRequestDTO;
import com.huiminpay.user.api.dto.tenant.TenantDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 王帅坤
 * @date 2021年06月07日16:48
 */
@Service
public class MerchantServiceImpl implements IMerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Reference
    TenantService tenantService;
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    StoreStaffMapper storeStaffMapper;

    @Override
    public MerchantDTO findMerchantById(Long id) {
        Merchant merchant = merchantMapper.selectById(id);
        MerchantDTO merchantDTO = new MerchantDTO();
//        BeanUtils.copyProperties(merchant,merchantDTO);
        return MerchantConvert.INSTANCE.entity2dto(merchant);
    }

    @Override
    public MerchantDTO regMch(MerchantDTO merchantDTO) {
//        Merchant merchant = new Merchant();
//        BeanUtils.copyProperties(merchantDto,merchant);
        // 1.手机号校验
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.lambda().eq(Merchant::getMobile,merchantDTO.getMobile());
        Integer integer = merchantMapper.selectCount(qw);
        if(integer>0){
            throw new BizException(CommonErrorCode.E_200203);
        }

        // 2.创建租户账号初始化权限
        //
        CreateTenantRequestDTO createAccountRequestDTO = new CreateTenantRequestDTO();
        ///
        createAccountRequestDTO.setMobile(merchantDTO.getMobile());
        createAccountRequestDTO.setPassword(merchantDTO.getPassword());
        createAccountRequestDTO.setUsername(merchantDTO.getUsername());
        createAccountRequestDTO.setTenantTypeCode("huimin-merchant");//huimin-merchant商户类型
        createAccountRequestDTO.setBundleCode("huimin-merchant");//
        createAccountRequestDTO.setName(merchantDTO.getUsername());
        TenantDTO tenantAndAccount = tenantService.createTenantAndAccount(createAccountRequestDTO);


        // 3.插入商户信息
        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        merchant.setAuditStatus("0");//审核状态 0-未申请,1-已申请待审核,2-审核通过,3-审核拒绝
        merchant.setTenantId(tenantAndAccount.getId());  // 更新租户ID
        merchantMapper.insert(merchant);


        // 4.  初始化门店和权限
        // 4.1.店铺 （根门店）
        Store store = new Store();
        store.setStoreName(merchantDTO.getUsername()+"001号");
        store.setMerchantId(merchant.getId());
        store.setStoreStatus(true);
        store.setParentId(0L);

        storeMapper.insert(store);

        // 4.2 添加员工
        Staff staff = new Staff();
        staff.setMerchantId(merchant.getId());
        staff.setUsername(merchantDTO.getUsername());
        staff.setMobile(merchantDTO.getMobile());
        staffMapper.insert(staff);

        // 4.3 给老板绑定门店管理权限
        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setStoreId(store.getId());
        storeStaff.setStaffId(staff.getId());
        storeStaffMapper.insert(storeStaff);

        merchantDTO.setId(merchant.getId());
        return merchantDTO;
    }

    @Override
    public MerchantDTO apply(MerchantDTO merchantDTO) {

//        Merchant merchant = new Merchant();
//        BeanUtils.copyProperties(merchantDTO,merchant);

        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);

        //审核状态修改
        merchant.setAuditStatus("1");
        merchantMapper.updateById(merchant);

        merchantDTO.setAuditStatus("1");
        return merchantDTO;
    }

    @Override
    public MerchantDTO queryMerchantByTenantId(Long tenantId) {
        QueryWrapper<Merchant> qw = new QueryWrapper();
        qw.lambda().eq(Merchant::getTenantId, tenantId);

        Merchant merchant = merchantMapper.selectOne(qw);

        return MerchantConvert.INSTANCE.entity2dto(merchant);

    }
}
